import _Form from "choerodon-ui/pro/lib/form";
import _CheckBox from "choerodon-ui/pro/lib/check-box";
import _TextField from "choerodon-ui/pro/lib/text-field";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _cloneDeep from "lodash/cloneDeep";
import _isEmpty from "lodash/isEmpty";
import _map from "lodash/map";
import React, { useContext, useEffect } from 'react';
import { observer } from 'mobx-react-lite';
import notification from "../../../utils/notification";
import SortTable from "./SortTable";
import QueryTable from "./QueryTable";
import ConditionTable from "./ConditionTable";
import Store from "./stores";
import { setCondition, setOrder, omitData } from "../utils";
var SearchModal = observer(function () {
  var _useContext = useContext(Store),
      searchId = _useContext.searchId,
      searchCode = _useContext.searchCode,
      tableDataSet = _useContext.tableDataSet,
      searchDataSet = _useContext.searchDataSet,
      listDataSet = _useContext.listDataSet,
      type = _useContext.type,
      modal = _useContext.modal,
      noCondition = _useContext.noCondition,
      noSort = _useContext.noSort,
      noQuery = _useContext.noQuery;

  function clearData(data) {
    var cloneData = _cloneDeep(data);

    cloneData.queryList = _map(cloneData.queryList, function (q) {
      return omitData(q);
    });
    cloneData.conditionList = _map(cloneData.conditionList, function (c) {
      return omitData(c);
    });
    cloneData.orderList = _map(cloneData.orderList, function (s) {
      return omitData(s);
    });
    return omitData(cloneData);
  }

  useEffect(function () {
    var record = listDataSet.find(function (r) {
      return r.get('searchId') === searchId;
    });

    if (['copy', 'edit'].includes(type)) {
      var searchName = record.get('searchName');
      if (type === 'edit') searchDataSet.loadData([record.toData()]);

      if (type === 'copy') {
        searchDataSet.create(clearData(record.toData()), 0);
        searchDataSet.current.set('searchName', "".concat(searchName, "_copy"));
      }
    } else {
      searchDataSet.create({
        searchCode: searchCode
      }, 0);
    }
  }, []);
  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var queryModified, preRecord, res, queryDataSet, _preRecord$toData, conditionList, orderList, currentId;

    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.prev = 0;
            queryModified = searchDataSet.children.queryList.dirty;
            preRecord = listDataSet.find(function (record) {
              return record.get('searchId') === searchId;
            });
            _context2.next = 5;
            return searchDataSet.submit();

          case 5:
            res = _context2.sent;

            if (!(!_isEmpty(res) && res.success)) {
              _context2.next = 24;
              break;
            }

            // 重置查询参数
            queryDataSet = tableDataSet.queryDataSet || tableDataSet.props.queryDataSet;
            if (queryDataSet) queryDataSet.reset();

            if (!preRecord) {
              _context2.next = 15;
              break;
            }

            _preRecord$toData = preRecord.toData(), conditionList = _preRecord$toData.conditionList, orderList = _preRecord$toData.orderList;
            _context2.next = 13;
            return setCondition(tableDataSet, conditionList, true);

          case 13:
            _context2.next = 15;
            return setOrder(tableDataSet, orderList, true);

          case 15:
            currentId = res.content[0].searchId;
            _context2.next = 18;
            return listDataSet.query();

          case 18:
            _context2.next = 20;
            return listDataSet.map( /*#__PURE__*/function () {
              var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(record) {
                var _record$toData, _conditionList, _orderList;

                return _regeneratorRuntime.wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        if (!(record.get('searchId') === currentId)) {
                          _context.next = 9;
                          break;
                        }

                        listDataSet.locate(record.index);
                        record.set('queryModified', queryModified);
                        _record$toData = record.toData(), _conditionList = _record$toData.conditionList, _orderList = _record$toData.orderList;
                        tableDataSet.setQueryParameter('search', searchCode);
                        _context.next = 7;
                        return setCondition(tableDataSet, _conditionList);

                      case 7:
                        _context.next = 9;
                        return setOrder(tableDataSet, _orderList);

                      case 9:
                        return _context.abrupt("return", null);

                      case 10:
                      case "end":
                        return _context.stop();
                    }
                  }
                }, _callee);
              }));

              return function (_x) {
                return _ref2.apply(this, arguments);
              };
            }());

          case 20:
            tableDataSet.query();
            return _context2.abrupt("return", true);

          case 24:
            if (!(res === undefined)) {
              _context2.next = 27;
              break;
            }

            notification.warning({
              message: '表单未做修改'
            });
            return _context2.abrupt("return", false);

          case 27:
            if (!_isEmpty(res) && res.failed && res.message) {
              notification.error({
                message: res.message
              });
            }

            return _context2.abrupt("return", false);

          case 31:
            _context2.prev = 31;
            _context2.t0 = _context2["catch"](0);
            return _context2.abrupt("return", false);

          case 34:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2, null, [[0, 31]]);
  })));
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_Form, {
    labelLayout: "horizontal",
    columns: 2,
    dataSet: searchDataSet
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: "searchName"
  }), /*#__PURE__*/React.createElement(_CheckBox, {
    name: "defaultFlag"
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "remark",
    colSpan: 2
  })), !noSort && /*#__PURE__*/React.createElement(SortTable, null), !noQuery && /*#__PURE__*/React.createElement(QueryTable, null), !noCondition && /*#__PURE__*/React.createElement(ConditionTable, null));
});
export default SearchModal;